// JavaScript Document

//Cookie判断用户是否登录
//设置cookie
function setCookie(cname,cvalue,exdays){
  var d = new Date();
  d.setTime(d.getTime()+(exdays*24*60*60*1000));
  var expires = "expires="+d.toGMTString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}
//读取cookie
function getCookie(cname){
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) 
  {
    var c = ca[i].trim();
    if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
  return "";
}
//删除
function delCookie(cname){
    document.cookie = cname+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
//判断是否存在
function isset(cname){
    return document.cookie.indexOf(cname+"=");
}


var uid = getCookie('uid');
var qq = getCookie('qq');

var flag_uid = isset('uid');
var flag_qq = isset('qq');
//console.log(document.cookie);
//说明已登陆
//更改图标
if(flag_uid!=-1){
    $(".logintext").html("<text><tspan x='0' y='42' font-size='48' line-height='0' fill='#000000' opacity='1' font-family='HappyZcool-2016' letter-spacing='0' ><a href=''>&nbsp;我要捐赠</a></tspan></text>");
    $(".regtext").html("");
    $("#loginbtn").html("<a href='?c=User&a=home' class='navbar__link'><img src='http://q.qlogo.cn/headimg_dl?dst_uin="+qq+"&spec=640' alt='Smiley face' width='42' height='42' style='border-radius: 20px;'><span>个人中心</span></a>");
    $("#donatebtn").html("<a href='?c=User&a=home' class='navbar__link' style='color:red;'><i class='material-icons md-light md-36 md-light' >favorite</i></i><span>我要捐赠</span></a>   ")
}

//页面加载时获取活动数据
//向页面载入活动信息
window.onload = function() {
	$.post("index.php?c=Index&a=getActivity",
	{},
	function(result){
	    //数据处理
	    var res = JSON.parse(result);
	    var html;
	    console.log(res);
	    //code不为200就是获取失败
	    if(res.code!=200){
	        $("#case").html("加载失败请刷新重试");
	    }
	    //获取成功
	    //结果集数组
	    var act_arr = res.act_arr;
	    var act_arr_len = act_arr.length;
	    if(act_arr_len>6){
	        act_arr_len = 6;
	    }
	    for(var i = 0;i<act_arr_len;i++){
	        //封面图地址
	        var head_img = act_arr[i]['pic'][0].image;
	        //拼装html
	        html = "<a  target='#'> <li> <img src='"+head_img+"' alt='"+act_arr[i].activity_name+"' class='lazy tran imgbig' data-original='"+head_img+"' style='display: none; transform: scale(1);'><div class='case-summary' style='display: none; opacity: 1;'><div style='position: relative;width:100%;height: 100%'><p class='p1'>"+act_arr[i].activity_name+"</p><p class='p2'><br><span class='syl'>捐书:"+act_arr[i].activity_book+"本</span><br> </p></div></div></li></a>";
	        //html = html.replace(/\'/,"\"");
	        //将其append到
	        $("#case").append(html);
	    }
	    //对append的元素重新绑定方法
	    $("#case").on('mouseenter','li',function(){
	        $(this).children(".case-summary").fadeIn(), $(this).children("img").css({
			transform: "scale(1.1)"});
	    });
	    $("#case").on('mouseleave','li',function(){
	        $(this).children(".case-summary").stop().fadeOut(), $(this).children("img").css({
			transform: "scale(1)"});
	    });
    });
}
